package com.dqp.cslggroup.ProgressButton;

import android.content.Context;
import android.os.Handler;

import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.NonNull;

import com.dd.morphingbutton.MorphingButton;
import com.dd.morphingbutton.impl.IndeterminateProgressButton;
import com.dd.morphingbutton.impl.LinearProgressButton;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;

public class LinearProgressButtonUtil extends BaseActivity {
    private static int mMorphCounter1 = 1;
    private static int mMorphCounter2 = 1;
    Context context;

    public LinearProgressButtonUtil(Context context) {
        this.context=context;
    }

    public  int dimen(@DimenRes int resId) {
        return (int) context.getResources().getDimension(resId);
    }

    public  int color(@ColorRes int resId) {
        return context.getResources().getColor(resId);
    }

    public  int integer(@IntegerRes int resId) {
        return context.getResources().getInteger(resId);
    }

    public  void LPonMorphButtonClicked(final LinearProgressButton btnMorph) {
            LPsimulateProgress1(btnMorph);
    }

    public  void LPmorphToSquare(final MorphingButton btnMorph, int duration) {
        MorphingButton.Params square = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_height_40))
                .width(dimen(R.dimen.mb_width_100))
                .height(dimen(R.dimen.mb_height_40))
                .color(color(R.color.mb_blue))
                .colorPressed(color(R.color.mb_blue_dark))
                .text("发布");
        btnMorph.morph(square);
    }

    public  void LPmorphToSuccess(final MorphingButton btnMorph) {
        MorphingButton.Params circle = MorphingButton.Params.create()
                .duration(integer(R.integer.mb_animation))
                .cornerRadius(dimen(R.dimen.mb_height_56))
                .width(dimen(R.dimen.mb_height_56))
                .height(dimen(R.dimen.mb_height_56))
                .color(color(R.color.mb_green))
                .colorPressed(color(R.color.mb_green_dark))
                .text("完成");
        btnMorph.morph(circle);
    }

    public  void LPmorphToFailure(final MorphingButton btnMorph, int duration) {
        MorphingButton.Params circle = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_height_56))
                .width(dimen(R.dimen.mb_height_56))
                .height(dimen(R.dimen.mb_height_56))
                .color(color(R.color.mb_red))
                .colorPressed(color(R.color.mb_red_dark))
                .text("失败");
        btnMorph.morph(circle);
    }

    public void  LPsimulateProgress1(@NonNull final LinearProgressButton button) {
        int progressColor = color(R.color.mb_purple);
        int color = color(R.color.mb_gray);
        int progressCornerRadius = dimen(R.dimen.mb_corner_radius_4);
        int width = dimen(R.dimen.mb_width_200);
        int height = dimen(R.dimen.mb_height_8);
        int duration = integer(R.integer.mb_animation);
        ProgressGenerator generator = new ProgressGenerator(button::unblockTouch);
        button.blockTouch(); // prevent user from clicking while button is in progress
        button.morphToProgress(color, progressColor, progressCornerRadius, width, height, duration);
        generator.start(button);
    }

    private void LPsimulateProgress2(@NonNull final LinearProgressButton button) {
        int progressColor = color(R.color.mb_purple);
        int color = color(R.color.mb_gray);
        int progressCornerRadius = dimen(R.dimen.mb_corner_radius_4);
        int width = dimen(R.dimen.mb_width_200);
        int height = dimen(R.dimen.mb_height_8);
        int duration = integer(R.integer.mb_animation);

        ProgressGenerator generator = new ProgressGenerator(button::unblockTouch);
        button.blockTouch(); // prevent user from clicking while button is in progress
        button.morphToProgress(color, progressColor, progressCornerRadius, width, height, duration);
        generator.start(button);
    }

    private void IPBonMorphButton1Clicked(final IndeterminateProgressButton btnMorph) {
        if (mMorphCounter1 == 0) {
            mMorphCounter1++;
            LPmorphToSquare(btnMorph, integer(R.integer.mb_animation));
        } else if (mMorphCounter1 == 1) {
            mMorphCounter1 = 0;
            IPBsimulateProgress1(btnMorph);
        }
    }
    public  void IPBmorphToSquare(final MorphingButton btnMorph, int duration) {
        MorphingButton.Params square = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_height_40))
                .width(dimen(R.dimen.mb_width_100))
                .height(dimen(R.dimen.mb_height_40))
                .color(color(R.color.mb_blue))
                .colorPressed(color(R.color.mb_blue_dark))
                .text("登录");
        btnMorph.morph(square);
    }

    public  void IPBmorphToSquare(final MorphingButton btnMorph, int duration,String text) {
        MorphingButton.Params square = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_height_40))
                .width(dimen(R.dimen.mb_width_100))
                .height(dimen(R.dimen.mb_height_40))
                .color(color(R.color.mb_blue))
                .colorPressed(color(R.color.mb_blue_dark))
                .text(text);
        btnMorph.morph(square);
    }
    public void IPBonMorphButtonClicked(final IndeterminateProgressButton btnMorph) {
            IPBsimulateProgress1(btnMorph);
    }

    public void IPBMorphToSquare(final IndeterminateProgressButton btnMorph, int duration,String name) {
        MorphingButton.Params square = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_corner_radius_2))
                .width(dimen(R.dimen.mb_width_100))
                .height(dimen(R.dimen.mb_height_56))
                .color(color(R.color.mb_blue))
                .colorPressed(color(R.color.mb_blue_dark))
                .text(name);
        btnMorph.morph(square);
    }

    public void IPBmorphToSuccess(final IndeterminateProgressButton btnMorph) {
        MorphingButton.Params circle = MorphingButton.Params.create()
                .duration(integer(R.integer.mb_animation))
                .cornerRadius(dimen(R.dimen.mb_height_56))
                .width(dimen(R.dimen.mb_height_56))
                .height(dimen(R.dimen.mb_height_56))
                .color(color(R.color.mb_green))
                .colorPressed(color(R.color.mb_green_dark))
                .icon(R.drawable.ic_action_accept);
        btnMorph.morph(circle);
    }

    public void IPBmorphToFailure(final IndeterminateProgressButton btnMorph, int duration) {
        MorphingButton.Params circle = MorphingButton.Params.create()
                .duration(duration)
                .cornerRadius(dimen(R.dimen.mb_height_56))
                .width(dimen(R.dimen.mb_height_56))
                .height(dimen(R.dimen.mb_height_56))
                .color(color(R.color.mb_red))
                .colorPressed(color(R.color.mb_red_dark))
                .icon(R.drawable.ic_action_cancel);
        btnMorph.morph(circle);
    }

    public void IPBsimulateProgress2(@NonNull final IndeterminateProgressButton button) {
        int progressColor = color(R.color.mb_blue);
        int color = color(R.color.mb_gray);
        int progressCornerRadius = dimen(R.dimen.mb_corner_radius_4);
        int width = dimen(R.dimen.mb_width_200);
        int height = dimen(R.dimen.mb_height_8);
        int duration = integer(R.integer.mb_animation);
        Handler handler = new Handler();
        handler.postDelayed(button::unblockTouch, 4000);
        button.blockTouch(); // prevent user from clicking while button is in progress
        button.morphToProgress(color, progressCornerRadius, width, height, duration, progressColor);
    }

    public void IPBsimulateProgress1(@NonNull final IndeterminateProgressButton button) {
        int progressColor1 = color(R.color.holo_blue_bright);
        int progressColor2 = color(R.color.holo_green_light);
        int progressColor3 = color(R.color.holo_orange_light);
        int progressColor4 = color(R.color.holo_red_light);
        int color = color(R.color.mb_gray);
        int progressCornerRadius = dimen(R.dimen.mb_corner_radius_4);
        int width = dimen(R.dimen.mb_width_200);
        int height = dimen(R.dimen.mb_height_8);
        int duration = integer(R.integer.mb_animation);

        Handler handler = new Handler();
        handler.postDelayed(button::unblockTouch, 4000);

        button.blockTouch(); // prevent user from clicking while button is in progress
        button.morphToProgress(color, progressCornerRadius, width, height, duration, progressColor1, progressColor2, progressColor3, progressColor4);
    }
}
